﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;

namespace ValidationLibrary
{
    public class ValidationRule<T>
    {
        public Expression<Func<T, bool>> RuleExpression { get; private set; }
        internal string _message { get; set; }
        internal List<PropertyInfo> _validatedProperties { get; set; }

        public ValidationRule(Expression<Func<T, bool>> rule)
        {
            RuleExpression = rule;
            _validatedProperties = new List<PropertyInfo>();
        }

        public ValidationRule<T> Message(string errorMessage)
        {
            _message = errorMessage;
            return this;
        }

        public ValidationRule<T> Validates(Expression<Func<T, object>> property)
        {
            _validatedProperties.Add(LambdaUtil.GetPropertyInfo(property));
            return this;
        }

        public ValidationRuleResult<T> Evaluate(T validatee)
        {
            return new ValidationRuleResult<T>(RuleExpression.Compile().Invoke(validatee), this);
        }
    }
}
